package com.zxl.live.screen.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxl.live.screen.R;

/**
 * Created by Administrator on 2017/11/14.
 */

public class WallpaperLoadErrorWidget extends LinearLayout implements View.OnClickListener {

    public static final int ERROR_NO_NET = 1;

    public static final int ERROR_REQUEST_NET = 2;

    private int type;

    private TextView msgView;

    private Button retryBtn;

    private OnLoadErrorListener mOnLoadErrorListener;

    public WallpaperLoadErrorWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        msgView = (TextView) findViewById(R.id.error_msg);
        retryBtn = (Button) findViewById(R.id.retry_button);
        retryBtn.setOnClickListener(this);
        findViewById(R.id.down_view).setOnClickListener(this);
        findViewById(R.id.error_feedback).setOnClickListener(this);
    }

    public void setOnLoadErrorListener(OnLoadErrorListener mOnLoadErrorListener) {
        this.mOnLoadErrorListener = mOnLoadErrorListener;
    }

    public void setType(int type) {
        this.type = type;
        switch (type) {
            case ERROR_NO_NET:
                msgView.setText(R.string.error_network_message);
                retryBtn.setText(R.string.error_setting);
                break;
            case ERROR_REQUEST_NET:
                msgView.setText(R.string.error_request_title);
                retryBtn.setText(R.string.error_network_refresh);
                break;
        }
        setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (null == mOnLoadErrorListener) {
            return;
        }
        switch (v.getId()) {
            case R.id.retry_button:
                switch (type) {
                    case ERROR_NO_NET:
                        mOnLoadErrorListener.onSetNetWorkClick();
                        break;
                    case ERROR_REQUEST_NET:
                        mOnLoadErrorListener.onRetryClick();
                        break;
                }
                break;
            case R.id.down_view:
                mOnLoadErrorListener.onDownloadManagerClick();
                break;
            case R.id.error_feedback:
                mOnLoadErrorListener.onFeedbackClick();
                break;
        }
        setVisibility(View.GONE);
    }

    public boolean isNetWorkError() {
        return type == ERROR_NO_NET;
    }

    public interface OnLoadErrorListener {

        void onSetNetWorkClick();

        void onRetryClick();

        void onDownloadManagerClick();

        void onFeedbackClick();

    }

}
